package base.loader {
	import flash.display.Sprite;
	import base.app.InitUtils;
	import flash.events.Event;
	import base.loader.show.LoaderShowSimple;
	import base.loader.show.LoaderProgress;
	import com.greensock.TweenNano;
	import com.greensock.easing.Linear;
	
	public class LoaderOneSwf extends Sprite {
		
		public static var COUNT_FRAMES_SHOW_HIDE: uint = 10;
		public static var COUNT_FRAMES_SHOW_SWF: uint = 10;
		
		private var pathSwf: String;
		protected var loaderShow: LoaderShowSimple;
		protected var swf: *;
		
		public function LoaderOneSwf(pathSwf: String): void {
			this.pathSwf = pathSwf;
			InitUtils.initApp(this);
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
		}
		
		private function onAddedToStage(e:Event = null): void {
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
			this.createLoaderShow();
		}
		
		protected function createProgress(): LoaderProgress {
			return null;
		}
		
		private function createLoaderShow(): void {
			this.loaderShow = new LoaderShowSimple(this.createProgress());
			this.loaderShow.alpha = 0;
			this.addChild(this.loaderShow);
			TweenNano.to(this.loaderShow, LoaderOneSwf.COUNT_FRAMES_SHOW_HIDE, {alpha: 1, useFrames: true, onComplete: this.loadSwf});
		}
		
		private function loadSwf(): void {
			this.swf = new LoaderExt(this.pathSwf, this.loadComplete, this.loaderShow.progress.onLoadProgress);
			this.swf.alpha = 0;
		}
			
		private function loadComplete(event:Event): void {
			if (this.loaderShow.labelDuration) this.loaderShow.labelDuration.stop();
			this.loaderShow.progress.setLoadProgress(1);
			this.hideLoaderShowAndShowSwf();
		}
		
		protected function hideLoaderShowAndShowSwf(): void {
			TweenNano.to(this.loaderShow, LoaderOneSwf.COUNT_FRAMES_SHOW_HIDE, {alpha: 0, useFrames: true, onComplete: this.removeLoaderShowAndShowSwf});
		}
		
		protected function initSwfVariables(): void {
			//np: this.swf.content["login"] = (root.loaderInfo.parameters.login) ? String(root.loaderInfo.parameters.login) : "";
		}
		
		private function removeLoaderShowAndShowSwf(): void {
			this.removeLoaderShow();
			this.showSwf(false);
		}
		
		private function removeLoaderShow(): void {
			this.loaderShow.destroy();
			this.removeChild(this.loaderShow);
			this.loaderShow = null;
		}
		
		protected function showSwf(isTweenShow: Boolean): void {
			this.initSwfVariables();
			this.addChild(this.swf);
			this.swf.alpha = 1;
			if (!isTweenShow) LoaderOneSwf.COUNT_FRAMES_SHOW_SWF = 0;
			TweenNano.from(this.swf.content, LoaderOneSwf.COUNT_FRAMES_SHOW_SWF, {alpha: 0, useFrames: true, ease: Linear.easeNone, onComplete: this.initSwf});
		}
		
		protected function initSwf(): void {
			this.destroy();
			if (this.swf.content.init) this.swf.content.init();
		}
		
		protected function destroy(): void {}
		
	}

}